package com.css.fxfzysx.modules.vaomeFacilities.repository.impl;

import com.css.fxfzysx.modules.lifeline.param.MajorProjectsParam;
import com.css.fxfzysx.modules.vaomeFacilities.entity.CipHisDamageEntity;
import com.css.fxfzysx.modules.vaomeFacilities.entity.CztMajorProjectsEntity;
import com.css.fxfzysx.modules.vaomeFacilities.repository.HeDamageRepository;
import com.css.fxfzysx.modules.vaomeFacilities.vo.MajorProjectAllVo;
import com.css.fxfzysx.modules.vaomeFacilities.vo.PageVo;
import com.css.fxfzysx.util.PlatformObjectUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Repository
public class HeDamageRepositoryImpl implements HeDamageRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void saveCipHisDamage(CipHisDamageEntity it) {
        jdbcTemplate.update("insert into czt_important_project_his_damage(ciphd_id,ciphd_code,seism_name,seism_time,magnitude,project_type,fortification,years,create_user,create_time,del_flag,params,break_state,user_name)" +
                "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",it.getCiphdId(),it.getCiphdCode(),it.getSeismName(),it.getSeismTime(),it.getMagnitude(),it.getProjectType(),it.getFortification(),it.getYears(),it.getCreateUser(),
                it.getCreateTime(),it.getDelFlag(),it.getParams(),it.getBreakState(),it.getUserName());
    }

    @Override
    public HashMap<String, Object> page(MajorProjectsParam param, int page, int size) {
        StringBuilder sql = new StringBuilder("select cp.*,cv.version_code from czt_major_projects cp left join czt_smx_version cv on cp.version_id = cv.id where cp.del_flag = '0' ");
        if(PlatformObjectUtils.isNotEmpty(param.getSeismName())){
            sql.append(" and cp.seism_name like '%" + param.getSeismName() + "%'");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getCreateUserName())){
            sql.append(" and cp.create_user_name like '%" + param.getCreateUserName() + "%'");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getYear())){
            if(param.getYear() == 1){
                sql.append(" and cp.year = '1978年以前' ");
            }else if(param.getYear() == 2){
                sql.append(" and cp.year  = '1978-1989年' ");
            }else if(param.getYear() == 3){
                sql.append(" and cp.year = '1989-2001年' ");
            }else if(param.getYear() == 4){
                sql.append(" and cp.year  = '2001年以后' ");
            }
        }
        if(PlatformObjectUtils.isNotEmpty(param.getMagnitude())){
            sql.append(" and cp.magnitude ='" + param.getMagnitude() + "'");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getProjectType())){
            sql.append(" and cp.project_type ='" + param.getProjectType() + "'");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getStartTime()) && PlatformObjectUtils.isNotEmpty(param.getEndTime())){
            sql.append(" and to_date( cp.seism_date || ' ' || cp.seism_time, 'YYYY-MM-DD HH24:MI:SS' ) between to_date( '"+param.getStartTime()+"', 'YYYY-MM-DD HH24:MI:SS' ) " +
                    " and to_date( '"+param.getEndTime()+"', 'YYYY-MM-DD HH24:MI:SS' )");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getFortification())){
            sql.append(" and cp.fortification ='" + param.getFortification() + "'");
        }
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(PageVo.class)).size();
        StringBuilder orderSql = new StringBuilder(" order by cp.create_time desc ");
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(orderSql).append(limitSql);
        List<PageVo> data = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(PageVo.class));
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", data);
        return resultMap;
    }

    @Override
    public CztMajorProjectsEntity getById(String id) {
        try {
            String sql = "select * from czt_major_projects where id = '"+id+"'";
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(CztMajorProjectsEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void deleteById(String id) {
        String sql = "update czt_major_projects set del_flag = '1' where id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateById(CztMajorProjectsEntity entity) {
        jdbcTemplate.update("UPDATE  czt_major_projects  SET seism_name=?,seism_date=?,seism_time=?,magnitude=?,project_type=?,longitude=?,latitude=?,year=?,destructive6_state=?,destructive7_state=?,destructive8_state=?,destructive9_state=?,destructive10_state=?,destructive11_state=?,destructive12_state=?,update_user=?,update_time=?,create_user_name=?"
                        +" where id=?",
                entity.getSeismName(),entity.getSeismDate(),entity.getSeismTime(),entity.getMagnitude(),entity.getProjectType(),entity.getLongitude(),entity.getLatitude(),entity.getYear(),entity.getDestructive6State(),entity.getDestructive7State(),entity.getDestructive8State(),entity.getDestructive9State(),entity.getDestructive10State(),entity.getDestructive11State(),entity.getDestructive12State(),entity.getUpdateUser(),entity.getUpdateTime(),entity.getCreateUserName(),
                entity.getId()
        );
    }

    @Override
    public List<MajorProjectAllVo> getByVersionId(String versionId) {
        String sql = "select id,seism_name,ST_AsText(geom) geom from czt_major_projects where del_flag = '0' and version_id = '"+versionId+"'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(MajorProjectAllVo.class));
    }
}
